package com.zr.beijian.Controller;

import com.zr.beijian.bean.BeiJian;
import com.zr.beijian.model.BeiJianSelectVo;
import com.zr.beijian.model.BeiJianUpdateVo;
import com.zr.beijian.model.BeiJianVo;
import com.zr.beijian.service.BeiJianService;
import com.zr.beijian.utils.Result;
import com.zr.beijian.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
public class BeiJianController {
    @Autowired
    private BeiJianService beiJianService;


    @PostMapping("/add")
    public Result add(@RequestBody @Valid BeiJianVo beiJianVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVo.error(500,bindingResult.getFieldError().getDefaultMessage());
        }
        return beiJianService.add(beiJianVo);
    }
    @GetMapping("/updatebefore")
    public BeiJian updatebefore(@RequestParam("id") int id){
        return beiJianService.findById(id);
    }
    @PostMapping("/update")
    public Result update (@RequestBody  @Valid BeiJianUpdateVo beiJianUpdateVo,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVo.error(500,bindingResult.getFieldError().getDefaultMessage());
        }
        return beiJianService.update(beiJianUpdateVo);
    }
    @GetMapping("/delete")
    public void delete (@RequestParam("id") int id){
         beiJianService.delete(id);
    }
    @PostMapping("/queryByCodeAndName")
    public Result queryByCodeAndName(@RequestBody BeiJianSelectVo beiJianSelectVo){
        return beiJianService.queryByCodeAndName(beiJianSelectVo);
    }
}
